package at.jku.pervasive.gui.player;

import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.Rectangle;
import javax.swing.JTextField;

import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.jlp;

import java.awt.Dimension;
import java.io.File;
import java.io.FilenameFilter;

import javax.swing.JLabel;

public class Mp3Player extends JFrame {

	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private JButton playButton = null;
	private JButton stopButton = null;
	private JTextField dirField = null;
	private jlp player = null;	
	private Mp3Play playThread = new Mp3Play();  //  @jve:decl-index=0:
	private JButton pauseButton = null;
	private boolean pause = false;
	private JButton nextButton = null;
	private JButton prevButton = null;
	private String[] musicFiles = null;
	private int aktPos = 0;
	private JLabel aktLabel = null;
	
	/**
	 * This is the default constructor
	 */
	public Mp3Player() {
		super();
		
		initialize();
		
//		musicFiles = new String[2];
//		musicFiles[0] = dirField.getText();
//		musicFiles[1] = secondFile.getText();
		
		aktPos = 0;
		
	    FilenameFilter filter = new FilenameFilter() {
	        public boolean accept(File dir, String name) {
	            return name.endsWith(".mp3");
	        }
	    };
		
		File dir = new File(dirField.getText());
		musicFiles = dir.list(filter);		
		
		for (int i = 0; i < musicFiles.length; i++)
			musicFiles[i] = dir.getAbsolutePath() + "\\" + musicFiles[i];
		
		aktLabel.setText("");
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(494, 126);
		this.setContentPane(getJContentPane());
		this.setTitle("Player");
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			aktLabel = new JLabel();
			aktLabel.setBounds(new Rectangle(15, 71, 451, 16));
			aktLabel.setText("JLabel");
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(getPlayButton(), null);
			jContentPane.add(getStopButton(), null);
			jContentPane.add(getDirField(), null);
			jContentPane.add(getPauseButton(), null);
			jContentPane.add(getNextButton(), null);
			jContentPane.add(getPrevButton(), null);
			jContentPane.add(aktLabel, null);
		}
		return jContentPane;
	}

	/**
	 * This method initializes playButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getPlayButton() {
		if (playButton == null) {
			playButton = new JButton();
			playButton.setBounds(new Rectangle(15, 40, 77, 21));
			playButton.setText("Play");
			playButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					playThread = new Mp3Play(musicFiles[aktPos]);
					playThread.start();
					System.out.println("play"); // TODO Auto-generated Event stub actionPerformed()
				}
			});
		}
		return playButton;
	}

	/**
	 * This method initializes stopButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getStopButton() {
		if (stopButton == null) {
			stopButton = new JButton();
			stopButton.setBounds(new Rectangle(94, 40, 77, 21));
			stopButton.setText("Stop");
			stopButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					playThread.stop();
					System.out.println("stop"); // TODO Auto-generated Event stub actionPerformed()
				}
			});
		}
		return stopButton;
	}

	/**
	 * This method initializes dirField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getDirField() {
		if (dirField == null) {
			dirField = new JTextField();
			dirField.setBounds(new Rectangle(15, 10, 457, 20));
			dirField.setText("C:\\MP3");
		}
		return dirField;
	}


	class Mp3Play extends Thread 
	{
		private String filename = "";
		
		public Mp3Play(String file) 
		{
			this.filename = file;
		}
		
		public Mp3Play() 
		{
		}
		
		public void run()
		{
			player = new jlp(filename);
			
			try {
				while (true)
				{
					aktLabel.setText("Playing ... " + filename);
					player.play();
				}
			} catch (JavaLayerException e1) {				
				e1.printStackTrace();
			}
		}
	}


	/**
	 * This method initializes pauseButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getPauseButton() {
		if (pauseButton == null) {
			pauseButton = new JButton();
			pauseButton.setBounds(new Rectangle(173, 40, 77, 21));
			pauseButton.setText("Pause");
			pauseButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					if (!pause)
					{
						playThread.suspend();
						pause = true;
					} else {
						playThread.resume();
						pause = false;
					}
											
					System.out.println("pause"); // TODO Auto-generated Event stub actionPerformed()
				}
			});
		}
		return pauseButton;
	}

	/**
	 * This method initializes nextButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getNextButton() {
		if (nextButton == null) {
			nextButton = new JButton();
			nextButton.setBounds(new Rectangle(255, 40, 77, 21));
			nextButton.setText("Next");
			nextButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					if (aktPos + 1 == musicFiles.length)
						aktPos = 0;
					else
						aktPos++;
					
					playThread.stop();
					playThread = new Mp3Play(musicFiles[aktPos]);
					playThread.start();
					
					System.out.println("next"); // TODO Auto-generated Event stub actionPerformed()
				}
			});
		}
		return nextButton;
	}

	/**
	 * This method initializes prevButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getPrevButton() {
		if (prevButton == null) {
			prevButton = new JButton();
			prevButton.setBounds(new Rectangle(336, 40, 87, 21));
			prevButton.setText("Previous");
			prevButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					if (aktPos - 1 < 0)
						aktPos = musicFiles.length - 1;
					else
						aktPos--;
					
					playThread.stop();
					playThread = new Mp3Play(musicFiles[aktPos]);
					playThread.start();
					
					System.out.println("previous"); // TODO Auto-generated Event stub actionPerformed()
				}
			});
		}
		return prevButton;
	}
	
}  //  @jve:decl-index=0:visual-constraint="10,10"


